<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            background-color: skyblue;
            height: 50px;
            width: 70px;
        }
    </style>
</head>
<body>
    <input type="text" class = "uname" placeholder="请输入用户名">
    <br>
    <span>输入用户名字数在6-16之间,且只能是大小写字母或数字，可以有下划线和横杠</span>

    <script>
        var uname = document.querySelector(".uname")
        // 指定正则表达式
        var reg = /^[a-zA-Z0-9_-]{6,16}$/
        //表单失去焦点时开始验证
        uname.onblur = function(){
            if(reg.test(this.value)){
                alert('用户名可以使用！')
            }else{
                alert('用户名格式有误！')
            }
        }
    </script>
</body>
</html>